package com.hm.pan.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hm.pan.model.User;
import com.hm.pan.model.ResultObj;
import com.hm.pan.service.AdUserService;

@RestController
@RequestMapping("user")
public class AdUserController {
	
	@Resource
	private AdUserService adUSerService;
	
	
	@RequestMapping("all")
	public Object findAll(String userName,Integer page){
		int tiao=(int) adUSerService.count();
		
		int yeshu=tiao%12==0?tiao/12:tiao/12+1;
		System.out.println("页数:"+yeshu);
		if(page!=null) {
			List<User> users = adUSerService.findByPage(page*12);
			for (User user : users) {
				user.setYeshu(yeshu);
			}
			if(!userName.isEmpty()&&userName!=null) {
				List<User> users1 = new ArrayList<>();
				User user = adUSerService.findByName(userName);
				users1.add(user);
				for (User user1 : users1) {
					user1.setYeshu(yeshu);
				}
				return new ResultObj(200,users1);
			} 
			return new ResultObj(200,users);
		}
		if(userName!=null) {
			List<User> users = new ArrayList<>();
			User user = adUSerService.findByName(userName);
			users.add(user);
			for (User user1 : users) {
				user1.setYeshu(yeshu);
			}
			return new ResultObj(200,users);
		}
			adUSerService.findAll();
			List<User> users = adUSerService.findByPage(0);
			for (User user1 : users) {
				user1.setYeshu(yeshu);
			}
			return new ResultObj(200,users);
		
	}
	
//	public Object findByPage(Integer page) {
//		List<User> users = adUSerService.findByPage(page);
//		return new ResultObj(200,users);
//	}
	

}
